package com.lambda.plugin;

import org.gradle.api.Plugin;
import org.gradle.api.Project;

import javax.annotation.Nonnull;

/**
 * Created by lambda7355 on 18-1-6.
 * gradle configuration
 * dimen {
 * targetScreenDp = [240, 320, 360, 390]
 * }
 */
public class DimensPlugin implements Plugin<Project> {
    @Override
    public void apply(@Nonnull Project project) {
        final DimensConfig config = project.getExtensions().create("dimensConfig", DimensConfig.class);
        // generateFlavor2DebugResources
        project.getTasks().whenTaskAdded(added -> {
            final String name = added.getName();
            if (name.startsWith("generate") && name.endsWith("Resources")) {
                final String variant = name.substring(8, name.length() - 9);
                if (!variant.endsWith("Test")) {
                    GenerateAction action = new GenerateAction(project, config);
                    action.setVariant(variant);
                    added.doLast(action);
                }
            }
        });
    }
}
